//
//  ReadingTableViewCell.m
//  村医
//
//  Created by 融医网 on 2017/6/7.
//  Copyright © 2017年 融医联盟. All rights reserved.
//

#import "YCReadingView.h"
@interface YCReadingView()<UITextViewDelegate>
@property(nonatomic,strong)UITextView *textView;
@end

@implementation YCReadingView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        UILabel *label = [[UILabel alloc]init];
        label.text = @"详细站点名称:";
        [self addSubview:label];
        
        UITextView *textView = [[UITextView alloc]init];
        self.textView = textView;
        self.siteNameTextView = textView;
        textView.scrollEnabled = NO;
        [textView setContentOffset:CGPointZero];
        textView.font = [UIFont systemFontOfSize:17];
        [self addSubview:textView];
        
        UILabel *placeholderLabel = [[UILabel alloc]init];
        self.placeholderLabel = placeholderLabel;
        placeholderLabel.text = @"请按照乡(镇)、村的格式填写";
        placeholderLabel.textColor = RGB(158, 158, 158);
        [self addSubview:placeholderLabel];
        
      
        UIView *viewLine = [[UIView alloc]init];
        viewLine.backgroundColor = RGB(240, 240, 240);
        [self addSubview:viewLine];

        
        __weak typeof(self) weakSelf = self;
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(weakSelf.mas_left).offset(0);;
            make.width.mas_equalTo(115);
            make.top.mas_equalTo(weakSelf.mas_top).offset(0);
            make.height.mas_equalTo(44);
        }];
        
        [textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(label.mas_right);
            make.right.mas_equalTo(weakSelf.mas_right).offset(0);
            make.top.mas_equalTo(weakSelf.mas_top).offset(4.5);
            make.height.mas_equalTo(34);
        }];
        
        [placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(label.mas_right).offset(6);
            make.right.mas_equalTo(weakSelf.mas_right).offset(0);
            make.top.mas_equalTo(weakSelf.mas_top).offset(4.5);
            make.height.mas_equalTo(34);
        }];
        
        [viewLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(weakSelf.mas_left).offset(0);
            make.right.mas_equalTo(weakSelf.mas_right).offset(0);
            make.bottom.mas_equalTo(weakSelf.mas_bottom).offset(-1);
            make.height.mas_equalTo(1);
        }];
        

    }
    return self;
}

-(void)setEndEditHandle:(EndEditHandle)endEditHandle{
    __weak typeof(self) weakSelf = self;
    self.siteNameTextView.delegate = weakSelf;
    _endEditHandle = endEditHandle;
}
-(void)textViewDidEndEditing:(UITextView *)textView{
    self.endEditHandle(textView.text);
}
-(void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length > 0) {
        
        BOOL isChange = NO;
        if (textView.text.length > 13) {
            isChange = YES;
            [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
               make.height.mas_equalTo(68);
            }];
        }else{
            [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(34);
            }];
            isChange = NO;
        }
        [[NSNotificationCenter defaultCenter]postNotificationName:@"kChangeLineLayout" object:@(isChange)];
        
        
        self.placeholderLabel.hidden = YES;
        if (textView.text.length > 25) {
            
            self.HandleTextViewText(@"最多输入25个字");
            textView.text = [textView.text substringToIndex:25];
            
        }
    }else if(textView.text.length == 0){
        
        self.placeholderLabel.hidden = NO;
    }
}

@end
